package me.zhengjie.modules.web.service.dis.impl;

import com.alibaba.fastjson.JSON;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.system.domain.DisOrder;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.web.domain.dis.DisOrderCalcDTO;
import me.zhengjie.modules.web.domain.user.AddCommissionDTO;
import me.zhengjie.modules.web.domain.user.GetUserSuperior;
import me.zhengjie.modules.web.enums.BillBizTypeEnum;
import me.zhengjie.modules.web.service.dis.IDisOrderWebService;
import me.zhengjie.modules.web.service.mybatisplus.IDisOrderPlusService;
import me.zhengjie.modules.web.service.mybatisplus.IUserAccountPlusService;
import me.zhengjie.modules.web.service.user.IUserWebService;
import me.zhengjie.modules.weixin.utils.OrderUtil;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

@Service
public class DisOrderWebServiceImpl implements IDisOrderWebService {

    @Autowired
    private IUserWebService userWebService;

    @Autowired
    private IDisOrderPlusService disOrderPlusService;

    @Autowired
    private IUserAccountPlusService accountPlusService;


    private static final Logger log = LoggerFactory.getLogger(DisOrderWebServiceImpl.class);
    @Override
    public boolean insertDisOrder(DisOrderCalcDTO calcDTO) {
        if (Objects.isNull(calcDTO)){
            return false;
        }
        if (Objects.isNull(calcDTO.getOrderType()) || Objects.isNull(calcDTO.getOrderAmount()) || calcDTO.getOrderAmount().compareTo(BigDecimal.ZERO) <= 0 || StringUtils.isAnyBlank(calcDTO.getOrderSn(),calcDTO.getOpenId())){
            return false;
        }
        GetUserSuperior userSuperior = null;
        try {
            userSuperior = userWebService.getUserSuperior(calcDTO.getOpenId());
        }catch (WeChatException e){
            log.warn("查询上级异常",e);
        }catch (Exception e) {
            log.error("查询上级异常",e);
        }
        if (Objects.isNull(userSuperior)){
            return false;
        }

        log.info("userSuperior="+ JSON.toJSONString(userSuperior));
        BigDecimal orderAmount = calcDTO.getOrderAmount();
        if (Objects.nonNull(userSuperior.getOneLevelRatio()) && userSuperior.getOneLevelRatio().compareTo(BigDecimal.ZERO) > 0){
            BigDecimal oneLevel = orderAmount.multiply(userSuperior.getOneLevelRatio());
            WxUser userInfo = userSuperior.getOneInfo();
            DisOrder disOrder = new DisOrder();
            String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Distribution.getPrefix());
            disOrder.setOrderNo(orderSn);
            disOrder.setOrderType(calcDTO.getOrderType());
            disOrder.setNickName(userInfo.getNickname());
            disOrder.setRealName(userInfo.getRealName());
            disOrder.setMobile(userInfo.getMobile());
            disOrder.setOpenId(userInfo.getOpenId());
            disOrder.setParentOpenId(userInfo.getSuperior());
            disOrder.setBizOrderNo(calcDTO.getOrderSn());
            disOrder.setBizOpenId(calcDTO.getOpenId());
            disOrder.setCommission(oneLevel);
            disOrder.setCreateTime(DateUtil.TimestampNow());
            disOrder.setBizOrderTitle(calcDTO.getTitle());
            boolean save = disOrderPlusService.save(disOrder);
            log.info("一级分销="+save);
            if (save){
                AddCommissionDTO dto = new AddCommissionDTO();
                dto.setOpenId(userInfo.getOpenId());
                dto.setOrderAmount(orderAmount);
                dto.setType(calcDTO.getOrderType());
                dto.setOrderSn(calcDTO.getOrderSn());
                dto.setAmount(oneLevel);
                boolean b = accountPlusService.addCommission(dto);
                log.info("一级分销账户入账结果="+b);
            }
        }
        if (Objects.nonNull(userSuperior.getTowLevelRatio()) && userSuperior.getTowLevelRatio().compareTo(BigDecimal.ZERO) > 0){
            BigDecimal twoLevel = orderAmount.multiply(userSuperior.getTowLevelRatio());
            WxUser userInfo = userSuperior.getTwoUser();
            DisOrder disOrder = new DisOrder();
            String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Distribution.getPrefix());
            disOrder.setOrderNo(orderSn);
            disOrder.setOrderType(calcDTO.getOrderType());
            disOrder.setNickName(userInfo.getNickname());
            disOrder.setRealName(userInfo.getRealName());
            disOrder.setMobile(userInfo.getMobile());
            disOrder.setOpenId(userInfo.getOpenId());
            disOrder.setParentOpenId(userInfo.getSuperior());
            disOrder.setBizOrderNo(calcDTO.getOrderSn());
            disOrder.setBizOpenId(calcDTO.getOpenId());
            disOrder.setCommission(twoLevel);
            disOrder.setCreateTime(DateUtil.TimestampNow());
            disOrder.setBizOrderTitle(calcDTO.getTitle());
            boolean save = disOrderPlusService.save(disOrder);
            log.info("二级分销="+save);

            AddCommissionDTO dto = new AddCommissionDTO();
            dto.setOpenId(userInfo.getOpenId());
            dto.setOrderAmount(orderAmount);
            dto.setType(calcDTO.getOrderType());
            dto.setOrderSn(calcDTO.getOrderSn());
            dto.setAmount(twoLevel);
            boolean b = accountPlusService.addCommission(dto);
            log.info("二级分销账户入账结果="+b);
        }
        return true;
    }
}
